package com.dcbeida.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.SalsesDetail;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 申请退货
 */
public class ApplyReturnAdapter extends BaseAdapter {

    private ArrayList<SalsesDetail.ListBean> mArrayList = new ArrayList<>();
    private Context mContext;

    private Boolean isSelected = false;
    private List<Integer> selecteds;//被选中的


    public List<Integer> getSelecteds() {
        return selecteds;
    }

    public Boolean getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(Boolean isSelected) {
        this.isSelected = isSelected;
    }

    /**
     * 清空选中集合
     */
    public void removeselected() {
        selecteds.clear();
    }

    public void removeSelected(int position) {
        if (selecteds.contains(position)) {
            selecteds.remove((Integer) position);
        }
    }

    public void removeAll() {
        selecteds.clear();
    }


    /**
     * 添加到选中集合
     *
     * @param position
     */
    public void addSelected(int position) {
        selecteds.add(position);

    }

    /**
     * 判断是否选择当前item
     *
     * @param position
     * @return
     */
    public boolean isItemSelected(int position) {
        return selecteds.contains(position) ? true : false;
    }


    public ApplyReturnAdapter(Context context) {
        super();
        this.mContext = context;

        selecteds = new ArrayList<Integer>();
    }


    @Override
    public int getCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return this.mArrayList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mArrayList == null) {
            return null;
        } else {
            return this.mArrayList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.apply_return_has_big_item, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SalsesDetail.ListBean bean = mArrayList.get(position);
        holder.tv_jgm.setText(bean.getDrugcode());
        holder.tv_num.setText(bean.getCount() + "");
        holder.tv_name.setText(bean.getDrugName());
        if (bean.isFirst()) {
            holder.lin_item_head.setVisibility(View.VISIBLE);
        } else {
            holder.lin_item_head.setVisibility(View.GONE);
        }
        int nextPos = position + 1;
        if (nextPos < getCount()) {
            SalsesDetail.ListBean nextBean = mArrayList.get(nextPos);
            if (nextBean.isFirst()) {
                holder.ll_line.setVisibility(View.VISIBLE);
            } else {
                holder.ll_line.setVisibility(View.GONE);
            }
        } else {
            holder.ll_line.setVisibility(View.VISIBLE);
        }


//        if (mArrayList.size()-1 == position) {
//            holder.ll_line.setVisibility(View.GONE);
//        } else {
//            holder.ll_line.setVisibility(View.VISIBLE);
//        }
//
        if (selecteds.contains(position)) {
            holder.iv_checked.setImageResource(R.drawable.selected);
        } else {
            holder.iv_checked.setImageResource(R.drawable.weixuanzhong);
        }

//        holder.iv_checked.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                if (isItemSelected(position)) {
//                    removeSelected(position);
//                } else {
//                    addSelected(position);
//                }
//                notifyDataSetChanged();
//            }
//        });


        return convertView;
    }


    public ArrayList<SalsesDetail.ListBean> getmArrayList() {
        return mArrayList;
    }

    public void setmArrayList(ArrayList<SalsesDetail.ListBean> mArrayList) {
        if (mArrayList.size() >= 0) {
            this.mArrayList.addAll(mArrayList);
        }
        notifyDataSetChanged();
    }

    public void clearList() {
        this.mArrayList.clear();
    }


    static class ViewHolder {
        @Bind(R.id.iv_checked)
        ImageView iv_checked;
        @Bind(R.id.lin_item_head)
        LinearLayout lin_item_head;
        @Bind(R.id.tv_jgm)
        TextView tv_jgm;
        @Bind(R.id.ll_line)
        LinearLayout ll_line;
        @Bind(R.id.tv_num)
        TextView tv_num;
        @Bind(R.id.tv_name)
        TextView tv_name;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
